package archive

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
    "github.com/flipped-aurora/gin-vue-admin/server/model/archive"
    archiveReq "github.com/flipped-aurora/gin-vue-admin/server/model/archive/request"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
    "github.com/flipped-aurora/gin-vue-admin/server/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
    "github.com/flipped-aurora/gin-vue-admin/server/utils"
)

type ArchiveLocationApi struct {
}

var archiveLocationService = service.ServiceGroupApp.ArchiveServiceGroup.ArchiveLocationService


// CreateArchiveLocation 创建档案存放位置
// @Tags ArchiveLocation
// @Summary 创建档案存放位置
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body archive.ArchiveLocation true "创建档案存放位置"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /archiveLocation/createArchiveLocation [post]
func (archiveLocationApi *ArchiveLocationApi) CreateArchiveLocation(c *gin.Context) {
	var archiveLocation archive.ArchiveLocation
	err := c.ShouldBindJSON(&archiveLocation)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    archiveLocation.CreatedBy = utils.GetUserID(c)

	if err := archiveLocationService.CreateArchiveLocation(&archiveLocation); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteArchiveLocation 删除档案存放位置
// @Tags ArchiveLocation
// @Summary 删除档案存放位置
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body archive.ArchiveLocation true "删除档案存放位置"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /archiveLocation/deleteArchiveLocation [delete]
func (archiveLocationApi *ArchiveLocationApi) DeleteArchiveLocation(c *gin.Context) {
	ID := c.Query("ID")
    	userID := utils.GetUserID(c)
	if err := archiveLocationService.DeleteArchiveLocation(ID,userID); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteArchiveLocationByIds 批量删除档案存放位置
// @Tags ArchiveLocation
// @Summary 批量删除档案存放位置
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /archiveLocation/deleteArchiveLocationByIds [delete]
func (archiveLocationApi *ArchiveLocationApi) DeleteArchiveLocationByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
    userID := utils.GetUserID(c)
	if err := archiveLocationService.DeleteArchiveLocationByIds(IDs,userID); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateArchiveLocation 更新档案存放位置
// @Tags ArchiveLocation
// @Summary 更新档案存放位置
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body archive.ArchiveLocation true "更新档案存放位置"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /archiveLocation/updateArchiveLocation [put]
func (archiveLocationApi *ArchiveLocationApi) UpdateArchiveLocation(c *gin.Context) {
	var archiveLocation archive.ArchiveLocation
	err := c.ShouldBindJSON(&archiveLocation)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    archiveLocation.UpdatedBy = utils.GetUserID(c)

	if err := archiveLocationService.UpdateArchiveLocation(archiveLocation); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindArchiveLocation 用id查询档案存放位置
// @Tags ArchiveLocation
// @Summary 用id查询档案存放位置
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query archive.ArchiveLocation true "用id查询档案存放位置"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /archiveLocation/findArchiveLocation [get]
func (archiveLocationApi *ArchiveLocationApi) FindArchiveLocation(c *gin.Context) {
	ID := c.Query("ID")
	if rearchiveLocation, err := archiveLocationService.GetArchiveLocation(ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"rearchiveLocation": rearchiveLocation}, c)
	}
}

// GetArchiveLocationList 分页获取档案存放位置列表
// @Tags ArchiveLocation
// @Summary 分页获取档案存放位置列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query archiveReq.ArchiveLocationSearch true "分页获取档案存放位置列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /archiveLocation/getArchiveLocationList [get]
func (archiveLocationApi *ArchiveLocationApi) GetArchiveLocationList(c *gin.Context) {
	var pageInfo archiveReq.ArchiveLocationSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := archiveLocationService.GetArchiveLocationInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}

// GetArchiveLocationPublic 不需要鉴权的档案存放位置接口
// @Tags ArchiveLocation
// @Summary 不需要鉴权的档案存放位置接口
// @accept application/json
// @Produce application/json
// @Param data query archiveReq.ArchiveLocationSearch true "分页获取档案存放位置列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /archiveLocation/getArchiveLocationPublic [get]
func (archiveLocationApi *ArchiveLocationApi) GetArchiveLocationPublic(c *gin.Context) {
    // 此接口不需要鉴权
    // 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
    response.OkWithDetailed(gin.H{
       "info": "不需要鉴权的档案存放位置接口信息",
    }, "获取成功", c)
}
